import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-http-bearer';
import { AuthService } from '../modules/user/services/auth.service';
import { AnonymousToken } from '../modules/common/constants/common.constant';

@Injectable()
export class JwtAnonymousStrategy extends PassportStrategy(Strategy, 'anonymous') {
  constructor(private readonly authService: AuthService) {
    super();
  }

  /**
   * 解析token
   * @param token token值
   */
  async validate(token: string) {
    if (!token) throw new UnauthorizedException();
    return token === AnonymousToken ? {} : await this.authService.decodeToken(token);
  }
}
